//
//  MantisWelcomeImgModel.m
//  NLProjectFrameworkDemo
//
//  Created by iosdeveloper on 2020/5/8.
//  Copyright © 2020 yj_zhang. All rights reserved.
//

#import "MantisWelcomeImgModel.h"
#import "UIImage+NLImageSize.h"
#import "UIView+ViewAnyCorners.h"
@interface MantisWelcomeImgModel()
@property(nonatomic,strong)MantisTextCellContentData*content;
@end

@implementation MantisWelcomeImgModel
-(CGSize)contentSize{
    CGFloat height=self.wkWebView.frame.size.height;
    NSString*msg=self.historyModel.msg;
    if (![msg containsString:@"additionMsg"]) {
        height+=0;
    }else
    {
        NSDictionary*contentDic=self.historyModel.msg.mj_keyValues;
        MantisTextCellContentData*content=[MantisTextCellContentData mj_objectWithKeyValues:contentDic];
        NSInteger btnCount=content.content.count;
        NSInteger rowCount=btnCount/3+btnCount%3;
        
        CGFloat addHeight=adaptWidth750(52)*rowCount+(rowCount-1)*5;
        
        height+=addHeight;
        
    }
    
    CGSize newSize=CGSizeMake(self.wkWebView.frame.size.width, height);
    
    return  newSize;
}

- (WKWebView *)wkWebView {
    if (!_wkWebView) {

//        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
//        NSString *jSString = [NSString stringWithFormat:@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=%f'); document.getElementsByTagName('head')[0].appendChild(meta);",SCREEN_WIDTH-adaptWidth750(120)-adaptWidth750(40)];
//        WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//        [wkWebConfig.userContentController addUserScript:wkUserScript];
        _wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(adaptWidth750(60*2),topMargin,IMSCREEN_WIDTH-adaptWidth750(140)-adaptWidth750(20), 1)];
        _wkWebView.navigationDelegate=self;
        _wkWebView.UIDelegate=self;
        _wkWebView.opaque = NO;
        _wkWebView.scrollView.scrollEnabled = NO;
        _wkWebView.scrollView.showsVerticalScrollIndicator = NO;
     
        
        NSString*msg=self.historyModel.msg;
        if (![msg containsString:@"additionMsg"]) {
            msg=self.historyModel.msg;
        }else
        {
            NSDictionary*contentDic=self.historyModel.msg.mj_keyValues;
            MantisTextCellContentData*content=[MantisTextCellContentData mj_objectWithKeyValues:contentDic];
            self.content=content;
            msg=content.additionMsg;
        }
        
//        NSString* codePrefixOne =@"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
//        codePrefixOne=[codePrefixOne stringByAppendingString:@"<html>"];
//        codePrefixOne=[codePrefixOne stringByAppendingString:@"<head>"];
//       codePrefixOne=[codePrefixOne stringByAppendingString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=\">"];
//        codePrefixOne=[codePrefixOne stringByAppendingString:@"<style type=\"text/css\">"];
//
//       NSString* codePrefixTwo=@"</style></head><body>";
//
//         NSString* codeSubfix=[codePrefixTwo stringByAppendingString:@"</body></html>"];
//
//        NSString*webMsg=[NSString stringWithFormat:@"%@%@%@%@%@",codePrefixOne,@"body{word-wrap:break-word;font-family:Arial}",msg,codePrefixTwo,codeSubfix];
        
        
//          msg.setShowContent(codePrefixOne + "body{word-wrap:break-word;font-family:Arial}" + codePrefixTwo + history.getMsg() + codeSubfix);
     
        
        
        
        
        NSString *htmlString = [NSString stringWithFormat:@"<html> \n"
                                "<head> \n"
                                "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no\"/>\n"
                                "<style type=\"text/css\"> \n"
                                "body {font-size:15px;word-wrap:break-word;font-family:Arial}\n"
                                "</style> \n"
                                "</head> \n"
                               "<body>\n"
                                "<script type='text/javascript'>"
                                "window.onload = function(){""}""}\n"
                                "</script>\n%@\n"
                                "</body>\n"
                                "</html>",msg];

        [_wkWebView loadHTMLString:htmlString baseURL:nil];
        if (@available(iOS 11.0,*)) {
            _wkWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _wkWebView.scrollView.bounces = NO;
        _wkWebView.backgroundColor = [UIColor whiteColor];
    }
    return _wkWebView;
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError *_Nullable error) {
        //获取页面高度
        CGFloat scrollHeight = [result doubleValue];
        scrollHeight=scrollHeight+topMargin;
        NSLog(@"scrollHeight 即为所求：%f",scrollHeight);
        CGRect webFrame = webView.frame;
             webFrame.size.height = scrollHeight;
             webView.frame = webFrame;
       // ViewRadius(webView, 4);
   UIColor*borderColor=[UIColor colorWithRed:6/255.0 green:203/255.0 blue:148/255.0 alpha:1.0];
    [webView setBorderWithCornerRadius:4 borderWidth:0 borderColor:borderColor type:UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight];

       // [webView sizeThatFits:webView.frame.size];
             
        [[NSNotificationCenter defaultCenter]postNotificationName:KnoticeWebLoadFinish object:nil];
    }];
    
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
  
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
   
}

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
    
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        
        NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
        
        completionHandler(NSURLSessionAuthChallengeUseCredential,card);
    }
}

//-(ChoiceBtnView *)choseBtnView{
//    if (!_choseBtnView) {
//        _choseBtnView =[[ChoiceBtnView alloc]initWithFrame:CGRectMake(self.wkWebView.jk_left,self.wkWebView.jk_bottom+adaptWidth750(40),self.wkWebView.jk_width , 0)];
//        [_choseBtnView reloadChoceBtnViewWith:self.content.content];
//    }
//    return _choseBtnView;
//}







+(NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{@"welComeId":@"_id"};
}




@end
